package com.example.chengche_lol.ui;

import android.content.Intent;
import android.os.Bundle;
import android.util.SparseArray;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.chengche_lol.R;
import com.example.chengche_lol.SetActivity;
import com.example.chengche_lol.adapter.MenuAdapter;
import com.example.chengche_lol.base.BaseActivity;
import com.example.chengche_lol.beans.MenuBean;
import com.example.chengche_lol.ui.circle.CircleFragment;
import com.example.chengche_lol.ui.friend.FriendFragment;
import com.example.chengche_lol.ui.mine.MineFragment;
import com.example.chengche_lol.ui.news.NewsMainFragment;
import com.example.chengche_lol.ui.store.StoreFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import java.util.ArrayList;
import java.util.List;
public class MainActivity extends BaseActivity {

    private DrawerLayout mDrawerLayout;

    private ImageView headView;
    private View scanView;
    private View moreView;
    private TextView userNameView;
    private TextView signatureView;
    private TextView levelView;
    private RecyclerView menuListView;
    private MenuAdapter mMenuAdapter;
    private View setting;

    private View rootContentView;
    private View drawerBtn;

    private BottomNavigationView mBottomNavigationView;
    private final SparseArray<Fragment> mFragmentCache = new SparseArray<>();


    @Override
    protected void initView() {
        mDrawerLayout = findViewById(R.id.drawer_layout);

        scanView = findViewById(R.id.scan_code);
        headView = findViewById(R.id.head_avatar);
        userNameView = findViewById(R.id.user_nick);
        moreView = findViewById(R.id.more_btn);
        levelView = findViewById(R.id.user_level);
        signatureView = findViewById(R.id.user_signature);
        menuListView = findViewById(R.id.rv_menu_list);

        setting = findViewById(R.id.setting_btn);

        rootContentView = findViewById(R.id.main_content);
        drawerBtn = findViewById(R.id.img_drawer_btn);

        mBottomNavigationView = findViewById(R.id.bottom_nav_view);
    }

    @Override
    protected int contentView() {
        return R.layout.activity_main;
    }

    @Override
    protected void initData() {
        menuListView.setLayoutManager(new LinearLayoutManager(getContext()));
        mMenuAdapter = new MenuAdapter();
        menuListView.setAdapter(mMenuAdapter);
    }


    @Override
    protected void bindData() {
        super.bindData();
        //定义头像、签名等信息
        userNameView.setText("好想吃蛋挞啊啊啊");
        levelView.setText("Lv39");
        signatureView.setText("术术人的专属花园，来说点什么吧");
        headView.setImageResource(R.drawable.avatar_socket);//侧边栏头像

        List<MenuBean> menuBeans = new ArrayList<>();
        menuBeans.add(new MenuBean(R.drawable.ic_baseline_message_24, "我的评论"));
        menuBeans.add(new MenuBean(R.drawable.ic_baseline_mail_24, "与我相关"));
        menuBeans.add(new MenuBean(R.drawable.ic_baseline_assignment_24, "我的订单"));
        menuBeans.add(new MenuBean(R.drawable.ic_baseline_check_box_24, "我的任务"));
        menuBeans.add(new MenuBean(R.drawable.ic_baseline_loyalty_24, "我的收藏"));
        menuBeans.add(new MenuBean(R.drawable.ic_baseline_card_giftcard_24, "活动中心"));
        menuBeans.add(new MenuBean(R.drawable.ic_baseline_verified_user_24, "信用分"));
        mMenuAdapter.addData(menuBeans);

        //实现点击菜单栏的吐司反馈
        mMenuAdapter.setOnItemClickListener(new MenuAdapter.OnItemClickListener() {
            @Override
            public void onClick(int position) {
                switch (position){
                    case 0:
                        Toast.makeText(MainActivity.this, "点击了" +"我的评论", Toast.LENGTH_SHORT).show();
                        break;
                    case 1:
                        Toast.makeText(MainActivity.this, "点击了" +"与我相关", Toast.LENGTH_SHORT).show();
                        break;
                    case 2:
                        Toast.makeText(MainActivity.this, "点击了" +"我的订单", Toast.LENGTH_SHORT).show();
                        break;
                    case 3:
                        Toast.makeText(MainActivity.this, "点击了" +"我的任务", Toast.LENGTH_SHORT).show();
                        break;
                    case 4:
                        Toast.makeText(MainActivity.this, "点击了" +"我的收藏", Toast.LENGTH_SHORT).show();
                        break;
                    case 5:
                        Toast.makeText(MainActivity.this, "点击了" +"活动中心", Toast.LENGTH_SHORT).show();
                        break;
                    case 6:
                        Toast.makeText(MainActivity.this, "点击了" +"信用分", Toast.LENGTH_SHORT).show();
                        break;
                    default:
                        Toast.makeText(MainActivity.this, "┗|｀O′|┛ 嗷~~", Toast.LENGTH_SHORT).show();
                }
            }
        });
        mMenuAdapter.setOnItemLongClickListener(new MenuAdapter.OnItemLongClickListener() {
            @Override
            public void onClick(int position) {
                Toast.makeText(MainActivity.this, "长按了" + position, Toast.LENGTH_SHORT).show();
            }
        });

        mBottomNavigationView.setOnNavigationItemSelectedListener(item -> {
            Fragment fragment = getContent(item);
            getSupportFragmentManager()
                    .beginTransaction()
                    .replace(R.id.fl_main_content, fragment)
                    .commit();
            return true;
        });
        mBottomNavigationView.setSelectedItemId(R.id.tab_news);
    }

    private  Fragment getContent(MenuItem menuItem){
        int id = menuItem.getItemId();
        Fragment fragment = mFragmentCache.get(id);
        if(fragment == null){
            fragment = nf(menuItem);
            mFragmentCache.put(id,fragment);
        }
        return fragment;
    }
//底部导航栏的切换
    private Fragment nf(MenuItem menuItem){
        Fragment fragment;
        switch (menuItem.getItemId()){
            case R.id.tab_news:
                fragment = new NewsMainFragment();
                break;
            case R.id.tab_circle:
                fragment = new CircleFragment();
                break;
            case R.id.tab_friend:
                fragment = new FriendFragment();
                break;
            case R.id.tab_store:
                fragment = new StoreFragment();
                break;
            case R.id.tab_user_center:
                fragment = new MineFragment();
                break;
            default:
                fragment = new NewsMainFragment();
        }
        return fragment;
    }

    @Override
    protected void bindEvent() {
        mDrawerLayout.addDrawerListener(new DrawerLayout.SimpleDrawerListener() {
            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {
                float diff = drawerView.getWidth() + drawerView.getX();
                rootContentView.setTranslationX(diff);
            }
        });

        setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "跳转设置", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(MainActivity.this, SetActivity.class);
                startActivity(intent);
            }
        });
        scanView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "扫描二维码", Toast.LENGTH_SHORT).show();
            }
        });
        drawerBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDrawerLayout.openDrawer(Gravity.LEFT);
            }
        });
        moreView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "查看更多", Toast.LENGTH_SHORT).show();
            }
        });

    }
}